/////////////////////////////////////////////////////////////////////////////
// Name:        preferences.cpp
// Purpose:     
// Author:      Stephen Whittle
// Modified by: 
// Created:     09/06/2009 16:39:41
// RCS-ID:      
// Copyright:   
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
#include "customcolours.h"
////@end includes
#include "globals.h"
#include "preferences.h"

////@begin XPM images
////@end XPM images


/*
 * Preferences type definition
 */

IMPLEMENT_DYNAMIC_CLASS( Preferences, wxDialog )


/*
 * Preferences event table definition
 */

BEGIN_EVENT_TABLE( Preferences, wxDialog )

////@begin Preferences event table entries
    EVT_BUTTON( ID_BUTTON, Preferences::OnDefineClick )

    EVT_CHECKBOX( ID_CHECKBOX2, Preferences::OnGaugeClick )

    EVT_BUTTON( wxID_OK, Preferences::OnOkClick )

////@end Preferences event table entries

END_EVENT_TABLE()


/*
 * Preferences constructors
 */

Preferences::Preferences()
{
    Init();
}

Preferences::Preferences( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
    Init();
    Create(parent, id, caption, pos, size, style);
}


/*
 * Preferences creator
 */

bool Preferences::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin Preferences creation
    SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
    wxDialog::Create( parent, id, caption, pos, size, style );

    CreateControls();
    if (GetSizer())
    {
        GetSizer()->SetSizeHints(this);
    }
    Centre();
////@end Preferences creation
    return true;
}


/*
 * Preferences destructor
 */

Preferences::~Preferences()
{
////@begin Preferences destruction
////@end Preferences destruction
}


/*
 * Member initialisation
 */

void Preferences::Init()
{
////@begin Preferences member initialisation
    m_CustomColours = NULL;
    m_Colour1 = NULL;
    m_Colour2 = NULL;
    m_FontPicker = NULL;
    m_LocalEcho = NULL;
    m_Gauges = NULL;
    m_GaugeString = NULL;
    m_SepChar = NULL;
////@end Preferences member initialisation
}


/*
 * Control creation for Preferences
 */

void Preferences::CreateControls()
{    
////@begin Preferences content construction
    Preferences* itemDialog1 = this;

    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
    itemDialog1->SetSizer(itemBoxSizer2);

    wxFlexGridSizer* itemFlexGridSizer3 = new wxFlexGridSizer(0, 1, 0, 0);
    itemBoxSizer2->Add(itemFlexGridSizer3, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

    wxStaticBox* itemStaticBoxSizer4Static = new wxStaticBox(itemDialog1, wxID_ANY, _("Colour Settings"));
    wxStaticBoxSizer* itemStaticBoxSizer4 = new wxStaticBoxSizer(itemStaticBoxSizer4Static, wxVERTICAL);
    itemFlexGridSizer3->Add(itemStaticBoxSizer4, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxFlexGridSizer* itemFlexGridSizer5 = new wxFlexGridSizer(0, 2, 0, 0);
    itemStaticBoxSizer4->Add(itemFlexGridSizer5, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

    m_CustomColours = new wxCheckBox( itemDialog1, ID_CHECKBOX, _("Use custom colours"), wxDefaultPosition, wxDefaultSize, 0 );
    m_CustomColours->SetValue(false);
    m_CustomColours->Enable(false);
    itemFlexGridSizer5->Add(m_CustomColours, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxButton* itemButton7 = new wxButton( itemDialog1, ID_BUTTON, _("Define Custom Colours"), wxDefaultPosition, wxDefaultSize, 0 );
    itemButton7->Enable(false);
    itemFlexGridSizer5->Add(itemButton7, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxFlexGridSizer* itemFlexGridSizer8 = new wxFlexGridSizer(0, 2, 0, 0);
    itemFlexGridSizer5->Add(itemFlexGridSizer8, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText9 = new wxStaticText( itemDialog1, wxID_STATIC, _("Default Text Colour"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer8->Add(itemStaticText9, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_Colour1 = new wxColourPickerCtrl( itemDialog1, ID_COLOURCTRL, wxColour(), wxDefaultPosition, wxDefaultSize, wxCLRP_DEFAULT_STYLE );
    itemFlexGridSizer8->Add(m_Colour1, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxFlexGridSizer* itemFlexGridSizer11 = new wxFlexGridSizer(0, 2, 0, 0);
    itemFlexGridSizer5->Add(itemFlexGridSizer11, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText12 = new wxStaticText( itemDialog1, wxID_STATIC, _("Background Colour"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer11->Add(itemStaticText12, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_Colour2 = new wxColourPickerCtrl( itemDialog1, ID_COLOURPICKERCTRL, wxColour(), wxDefaultPosition, wxDefaultSize, wxCLRP_DEFAULT_STYLE );
    itemFlexGridSizer11->Add(m_Colour2, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticBox* itemStaticBoxSizer14Static = new wxStaticBox(itemDialog1, wxID_ANY, _("Font"));
    wxStaticBoxSizer* itemStaticBoxSizer14 = new wxStaticBoxSizer(itemStaticBoxSizer14Static, wxVERTICAL);
    itemFlexGridSizer3->Add(itemStaticBoxSizer14, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_FontPicker = new wxFontPickerCtrl( itemDialog1, ID_FONTCTRL, wxFont(), wxDefaultPosition, wxDefaultSize, wxFNTP_DEFAULT_STYLE|wxFNTP_USE_TEXTCTRL|wxFNTP_USEFONT_FOR_LABEL );
    itemStaticBoxSizer14->Add(m_FontPicker, 0, wxGROW|wxALL, 5);

    m_LocalEcho = new wxCheckBox( itemDialog1, ID_CHECKBOX1, _("Echo Commands Locally"), wxDefaultPosition, wxDefaultSize, 0 );
    m_LocalEcho->SetValue(false);
    itemFlexGridSizer3->Add(m_LocalEcho, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_Gauges = new wxCheckBox( itemDialog1, ID_CHECKBOX2, _("Display HP gauges"), wxDefaultPosition, wxDefaultSize, 0 );
    m_Gauges->SetValue(false);
    m_Gauges->Enable(false);
    itemFlexGridSizer3->Add(m_Gauges, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxFlexGridSizer* itemFlexGridSizer18 = new wxFlexGridSizer(0, 2, 0, 0);
    itemFlexGridSizer18->AddGrowableCol(1);
    itemFlexGridSizer3->Add(itemFlexGridSizer18, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText19 = new wxStaticText( itemDialog1, wxID_STATIC, _("Pattern"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer18->Add(itemStaticText19, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_GaugeString = new wxTextCtrl( itemDialog1, ID_TEXTCTRL8, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
    m_GaugeString->Enable(false);
    itemFlexGridSizer18->Add(m_GaugeString, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxFlexGridSizer* itemFlexGridSizer21 = new wxFlexGridSizer(0, 2, 0, 0);
    itemFlexGridSizer3->Add(itemFlexGridSizer21, 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText22 = new wxStaticText( itemDialog1, wxID_STATIC, _("Command seperator character"), wxDefaultPosition, wxDefaultSize, 0 );
    itemFlexGridSizer21->Add(itemStaticText22, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_SepChar = new wxTextCtrl( itemDialog1, ID_TEXTCTRL11, wxEmptyString, wxDefaultPosition, wxSize(20, -1), 0 );
    m_SepChar->SetMaxLength(1);
    itemFlexGridSizer21->Add(m_SepChar, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStdDialogButtonSizer* itemStdDialogButtonSizer24 = new wxStdDialogButtonSizer;

    itemFlexGridSizer3->Add(itemStdDialogButtonSizer24, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5);
    wxButton* itemButton25 = new wxButton( itemDialog1, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStdDialogButtonSizer24->AddButton(itemButton25);

    wxButton* itemButton26 = new wxButton( itemDialog1, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStdDialogButtonSizer24->AddButton(itemButton26);

    itemStdDialogButtonSizer24->Realize();

////@end Preferences content construction
m_Colour1->SetColour(pConfig->Read(wxT("/Colours/DefaultText"),wxT("white")));
m_Colour2->SetColour(pConfig->Read(wxT("/Colours/DefaultBackground"),wxT("black")));
m_FontPicker->SetLabel(wxT("Sample"));
wxFont defaultFont;
defaultFont.SetNativeFontInfoUserDesc(pConfig->Read(wxT("/Font/Default"),wxT("courier new 10 windows-1252")));
m_FontPicker->SetSelectedFont(defaultFont);
bool doEcho;
pConfig->Read(wxT("/Terminal/LocalEcho"), &doEcho, true);
m_LocalEcho->SetValue(doEcho);
bool showGauges;
pConfig->Read(wxT("/UI/ShowGauges"), &showGauges, false);
m_Gauges->SetValue(showGauges);
m_GaugeString->SetValue(pConfig->Read(wxT("/UI/GaugeString"), wxEmptyString));
m_GaugeString->Enable(showGauges);
m_SepChar->SetValue(pConfig->Read(wxT("/Terminal/CommandSeperator"),wxT(";")));
}


/*
 * Should we show tooltips?
 */

bool Preferences::ShowToolTips()
{
    return true;
}

/*
 * Get bitmap resources
 */

wxBitmap Preferences::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin Preferences bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end Preferences bitmap retrieval
}

/*
 * Get icon resources
 */

wxIcon Preferences::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin Preferences icon retrieval
    wxUnusedVar(name);
    return wxNullIcon;
////@end Preferences icon retrieval
}


/*
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_OK
 */

void Preferences::OnOkClick( wxCommandEvent& event )
{
pConfig->SetPath(wxT("/Colours"));
pConfig->Write(_T("DefaultText"),(m_Colour1->GetColour()).GetAsString());
pConfig->Write(_T("DefaultBackground"),(m_Colour2->GetColour()).GetAsString());
pConfig->Write(wxT("UseCustomColours"),m_CustomColours->IsChecked());
pConfig->Write(wxT("/Font/Default"),(m_FontPicker->GetSelectedFont()).GetNativeFontInfoUserDesc());
pConfig->Write(wxT("/Terminal/LocalEcho"), m_LocalEcho->IsChecked());
pConfig->Write(wxT("/Terminal/CommandSeperator"),m_SepChar->GetValue());

pConfig->Flush();

wxFont defaultFont;
wxTextAttr defaultStyle(wxColour(pConfig->Read(wxT("/Colours/DefaultText"),wxT("rgb(255, 255, 255)"))), 
    wxColour(pConfig->Read(wxT("/Colours/DefaultBackground"),wxT("rgb(0, 0, 0)"))), defaultFont);
setDefaultFont(defaultStyle);
setBackgroundColour(wxColour(pConfig->Read(wxT("/Colours/DefaultBackground"),wxT("rgb(0, 0, 0)"))));
this->EndModal(wxID_OK); 
}


/*
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BUTTON
 */

void Preferences::OnDefineClick( wxCommandEvent& event )
{
////@begin wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BUTTON in Preferences.
    // Before editing this code, remove the block markers.
    CustomColours* window = new CustomColours(this);
    int returnValue = window->ShowModal();
    window->Destroy();
////@end wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BUTTON in Preferences. 
}


/*
 * wxEVT_COMMAND_CHECKBOX_CLICKED event handler for ID_CHECKBOX2
 */

void Preferences::OnGaugeClick( wxCommandEvent& event )
{
 m_GaugeString->Enable(m_Gauges->IsChecked());
}

